﻿CREATE PROCEDURE [dbo].[s_PromisesToPay_Update] /*WITH EXECUTE AS 'AFC-HOME\sqlserver'*/
WITH EXECUTE AS 'AFC-HOME\SQLServer'
 AS
SET NOCOUNT ON

DECLARE @SQL varchar(max)

UPDATE t_Note SET StatusID = dbo.fs_StateID__Notes__PaymentRecieved()--,StatusDate = dbo.MinDate(GETDATE(),DateDue)
WHERE StatusID IN (dbo.fs_StateID__Notes__PromisedToPay(),dbo.fs_StateID__Notes__BrokenPromise())
AND dbo.PaymentsByNoteAndDueDates_Amount(LoanID,NoteDate0,dbo.clr_Date(DateDue))>0

SET @SQL = CONVERT(varchar,@@ROWCOUNT) + ' Paid'

DECLARE @Today datetime SET @Today = dbo.clr_Date(GETDATE())
UPDATE t_Note SET StatusID = dbo.fs_StateID__Notes__BrokenPromise()--,StatusDate = dbo.MinDate(GETDATE(),DateDue)
WHERE StatusID = dbo.fs_StateID__Notes__PromisedToPay()
 AND dbo.clr_BusinessDaysDiff(DateDue,@Today,1) > 1
 AND dbo.PaymentsByNoteAndDueDates_Amount(LoanID,NoteDate0,dbo.clr_Date(DateDue)) <= 0

SET @SQL ='SELECT '''+ @SQL +','+ CONVERT(varchar,@@ROWCOUNT) + ' Broken'' AS Result'

EXEC admin.s_Mail_System 's_PromisesToPay_Update','',@SQL




